package thread;

public class TestSync {
    public static void main(String[] args) {

        Bank b=new Bank();
        Thread t1=new Thread(){
            public void run() {
                b.getoney();
            }
        };

        Thread t2=new Thread(){
            public void run() {
                b.getoney();
            }
        };
    }
}

class Bank{
    private int money=1000;
    static Object obj=new Object();
    public  void getoney(){


        synchronized (obj){//对象同步监视器对象
            if (money < 0) {
                throw new RuntimeException("没钱了，不能取了");
            }
            money--;
        }



    }
}














